package com.hbpa.daoImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.hbpa.dao.StudentJyDao;

import com.hbpa.form.StudentJyItem;
import com.hbpa.util.ConnectionFactory;
import com.hbpa.util.JdbcUtil;
 
public class StudentJyDaoImpl implements StudentJyDao {

	public boolean addStudentJy(String xh,String szd,String dw,String sj,String dwdh) {
		Connection con = null;
		PreparedStatement ps = null;
		try {
			con = ConnectionFactory.getConnection();
			con.setAutoCommit(false);
			ps = con.prepareStatement("insert into st_jy(xh,szd,dw,sj,dwdh) values(?,?,?,?,?)");
			ps.setString(1, xh);
			ps.setString(2, szd); 
			ps.setString(3, dw);
			ps.setString(4, sj); 
			ps.setString(5, dwdh); 
			ps.executeUpdate();
			con.commit();
			return true;
		} catch (SQLException e) {
			if(con!=null){try{con.rollback();}catch(Exception ex){}}
			e.printStackTrace();
		}finally{
			JdbcUtil.close(ps, con);
		}
		return false;
	}

	public boolean checkStudentJy(String xh) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			con = ConnectionFactory.getConnection(); 
			ps = con.prepareStatement("select count(*) as c from st_jy where xh = ?");
			
			ps.setString(1, xh);
			rs = ps.executeQuery();
			if(rs.next())
			{ 
				String count = rs.getString("c");
				if(!count.equals("0")){
					return true;
				}else{
					return false;
				}
			}
		} catch (SQLException e) { 
			e.printStackTrace();
		}finally{
			JdbcUtil.close(rs,ps,con);
		}
		return false;
	}

	
	public boolean deleteAllStudentJy(String xh) {
		Connection con = null;
		PreparedStatement ps = null;
		try {
			con = ConnectionFactory.getConnection();
			con.setAutoCommit(false);
			ps = con.prepareStatement("delete from st_jy where xh=?");
			ps.setString(1,xh);
			
			ps.execute();
			con.commit();
			return true;
		} catch (SQLException e) {
			if(con!=null){try{con.rollback();}catch(Exception ex){}}
			e.printStackTrace();
		}finally{
			JdbcUtil.close(ps, con);
		}
		return false;
	}
	
	
	public boolean deleteStudentJy(String id, String xh) {
		Connection con = null;
		PreparedStatement ps = null;
		try {
			con = ConnectionFactory.getConnection();
			con.setAutoCommit(false);
			ps = con.prepareStatement("delete from st_jy where id=? and xh=?");
			ps.setString(1, id);
			ps.setString(2, xh);
			
			ps.execute();
			con.commit();
			return true;
		} catch (SQLException e) {
			if(con!=null){try{con.rollback();}catch(Exception ex){}}
			e.printStackTrace();
		}finally{
			JdbcUtil.close(ps, con);
		}
		return false;
	}

	
	public List<StudentJyItem> getStudentJyByXh(String xh) {
		if(checkStudentJy(xh)==false)
		{
			return null;
		}
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null; 
		List<StudentJyItem> StudentJyItem = new ArrayList<StudentJyItem>();
		try {
			con = ConnectionFactory.getConnection(); 
			ps = con.prepareStatement("select * from st_jy where xh = ? order by id asc");

			ps.setString(1, xh);
			rs = ps.executeQuery();
			while(rs.next())
			{   
				StudentJyItem item = new StudentJyItem();
				item.setId(rs.getString("id"));
				item.setXh(rs.getString("xh"));
				item.setDw(rs.getString("dw"));
				item.setDwdh(rs.getString("dwdh"));
				item.setSj(rs.getString("sj"));
				item.setSzd(rs.getString("szd"));
				StudentJyItem.add(item);
			}
			 return StudentJyItem;
		} catch (SQLException e) { 
			e.printStackTrace();
		}finally{
			JdbcUtil.close(rs,ps,con);
		} 
		return null;
	}


}
